package ui.adapter;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fy.fyzf.R;
import com.fy.fyzf.bean.HousesListBean;
import com.fy.fyzf.bean.HousesListBean1;
import com.fy.fyzf.glideUtils.GlideUtils;
import com.fy.fyzf.utils.MathUtils;
import com.fy.fyzf.utils.StringUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.litepal.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

public class BuildingDetailAdapter extends BaseQuickAdapter<HousesListBean1, BaseViewHolder> {

    public BuildingDetailAdapter() {
        super(R.layout.item_building_detail);
    }

    @Override
    protected void convert(BaseViewHolder helper, HousesListBean1 item) {
        TagFlowLayout tagFlowLayout = helper.getView(R.id.tagflow);
        List<String> list = new ArrayList<>();
        switch (item.getRenovationType()) {
            case 1:
                list.add("简装");
                break;
            case 2:
                list.add("精装");
                break;
            case 3:
                list.add("豪装");
                break;
        }
        if (item.getBigOwner() == 1) {
            list.add("大业主");
        }

        String s = "";
      /*  String rentPrice;
        if (null == item.getRentPrice()) {
            rentPrice = "0.0";
        } else {
            rentPrice = item.getRentPrice();
        }*/
        int length = 0;
        switch (item.getHousingPurpose()) {
            case 0:
                list.add("租");
                if (item.getRentPrice() == null) {
                    s =  "0元/m²/天";
                    length =1;
                } else {
                    length = item.getRentPrice().length();
                    s = item.getRentPrice() + "元/m²/天";

                }
                break;
            case 1:
                list.add("售");
                if (item.getSellPrice() == null) {
                    s = "0元/m²/平";
                    length =1;
                } else {
                    length = item.getSellPrice().length();
                    s = item.getSellPrice() + "元/m²";

                }
                break;
            case 2:
                list.add("租");
                list.add("售");
                if (item.getRentPrice() == null) {
                    s =  "0元/m²/天";
                    length =1;
                } else {
                    length = item.getRentPrice().length();
                    s = item.getRentPrice() + "元/m²/天";
                }
                break;
        }
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(s);
        String[] split = s.split("元");
        String s1 = "元" + split[1];
        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#FF7E00")), 0, split[0].length(), spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
//        spannableStringBuilder.setSpan(new ForegroundColorSpan(Color.parseColor("#353535")),  split[0].length(), s1.length(), spannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
        helper.setText(R.id.tv_rent_price, spannableStringBuilder);
        helper.setText(R.id.tv_name, item.getHousingNo() + "室  |  " + MathUtils.rvZeroAndDot(item.getHousingArea()) + "m²");

        if (null == item.getHousingImages()) {
            GlideUtils.loadRoundedCornersImage_Local(R.mipmap.housing_nodata,helper.getView(R.id.logo),5);
        } else {
            GlideUtils.loadRoundedCornersImage(item.getHousingImages(),helper.getView(R.id.logo),5);
        }
        tagFlowLayout.setAdapter(new TagAdapter<String>(list) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_peer_tab,
                        tagFlowLayout, false);
                GradientDrawable gradientDrawable = (GradientDrawable) tv.getBackground();
                if (list.get(position).equals("大业主") ||list.get(position).equals("简装") ||list.get(position).equals("精装") || list.get(position).equals("豪装")) {
                    gradientDrawable.setColor(Color.parseColor("#C3E3FC"));
                    tv.setTextColor(Color.parseColor("#457FFF"));
                } else if (list.get(position).equals("租")) {
                    gradientDrawable.setColor(Color.parseColor("#FED5AD"));
                    tv.setTextColor(Color.parseColor("#FF7E00"));
                } else {
                    gradientDrawable.setColor(Color.parseColor("#CEEAC2"));
                    tv.setTextColor(Color.parseColor("#5DB835"));
                }
                tv.setText(list.get(position));
                return tv;
            }
        });
    }
}
